var salt = sessionStorage.getItem('usersalt');

layui.use(['form', 'layer', 'upload', 'laytpl'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laytpl = layui.laytpl,
		upload = layui.upload;
		
	if(!sessionStorage.getItem('username')) {
		layer.msg('请先登录');
		setTimeout("top.location.href='../../../index.html'", 1000);
	}
    var index;
	/*------------------上传文件.start------------------*/
	var fileNum = 0,
		fileArr = new Array();

	upload.render({
		elem: '#courseData',
		url: url + 'attachment/upload',
		multiple: true,
		accept: 'file',
		exts: 'ppt|pptx|webm|avi|mp4|ogg|swf',//ppt与视频文件
		before: function(obj) {
            index = layer.load(1, {
                shade: [0.3,'#000'] //0.1透明度的白色背景
            });
		},
		done: function(res) {
			//上传完毕
			if (res.code == 0) {
                layer.close(index);
				fileNum++;
				fileArr = new Array();
				fileArr.push(res.data.id);

				var fileHtml = $('<div style="display:inline-block;position:relative;margin-right:45px;" id="picupfile' + fileNum + '"  ><div onclick="delfile(\'picupfile' + fileNum + '\',\'' + fileNum + '\',\'' + res.data.id + '\')" style="position:absolute;top:-2px;right:-21px;color:red;width:16px;height:16px"><i class="layui-icon" style="position:relative;top:-2px;right:0px;">&#x1006;</i></div><a style="color: blue;text-decoration: underline;" href="' + url + 'attachment/seekFile/' + res.data.id + '" >' + res.data.originaName + '</a></div>');
				$('#dataDiv').html(fileHtml);
				
				if (res.data.fileExt == "ppt" || res.data.fileExt == "pptx") {
					isPpt = true;
					// changePptToImg(res.data.id);
				}
			} else {
				layer.msg(res.msg);
			}
		}
	});
	/*------------------上传文件.end------------------ */
	
	/*------------------删除文件.start------------------*/
	window.delfile = function(self, num, inde) {
		layer.confirm('你确定要删除吗？', {
			btn: ['确定', '取消'] //按钮
		}, function() {
			$('#' + self).remove();
			fileArr = new Array();
			layer.msg('删除成功');
		}, function() {
	
		});
	}
	/*------------------删除文件.end------------------ */
	
	/*------------------上传的PPT转成图片.start------------------*/
	var isPpt = false;
	var isPptImgIds = "";
	function changePptToImg(fileId){
		$.ajax({
			url: url + 'attachment/changePptToImg',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {
				id: fileId
			},
			success: function(data) {
				if (data.code == 0) {
					isPptImgIds = data.data.join(",");
				} else {
					layer.msg(data.msg);
				}
			},
			error: function(error) {
				console.log("获取失败");
			}
		});
	}
	/*------------------上传的PPT转成图片.start------------------*/
	
	/*------------------取出所有的业务分类.start------------------*/
	var post;
	function getParentPost() {
		$.ajax({
			url: url + 'post/getParentPost',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {
			},
			success: function(data) {
				if(data.code == 0) {
					post = data.data;
	
					/*------------------选择岗位渲染.start------------------*/
					var postType = $("#postType").html();
					laytpl(postType).render(post, function(html){
					  	$("#postTypeView").html(html);
					  	
					  	//html点击事件
					  	$("body").click(function() {
							$("#postSelect").hide();
						})
						$('#postInput').click(function() {
							$("#postSelect").show();
							return false;
						});
					});
					/*------------------选择岗位渲染.end------------------*/
				} else {
					layer.msg(data.msg);
				}
			},
			error: function(error) {
				console.log("获取失败");
			}
		});
	}
	getParentPost();
	
	//选择岗位业务
	window.choosePostType = function(id, name){
		$("#postTypeId").val(id);
		$("#postTypeName").val(name);
		$(".postType").removeClass("layui-this");
		$("#postType" + id).addClass("layui-this");
	};
	/*------------------取出所有的业务分类.end------------------*/
	
	/*------------------取出所有的课程分类.start------------------*/
	var course;
	function getCourseType() {
		$.ajax({
			url: url + 'course/getCourseType',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {
			},
			success: function(data) {
				if (data.code == 0) {
					course = data.data;
	
					/*------------------选择课程渲染.start------------------*/
					var courseType = $("#courseType").html();
					laytpl(courseType).render(course, function(html){
					  	$("#courseTypeView").html(html);
					  	
					  	//html点击事件
					  	$("body").click(function() {
							$("#courseSelect").hide();
						})
						$('#courseInput').click(function() {
							$("#courseSelect").show();
							return false;
						});
					});
					/*------------------选择课程渲染.end------------------*/
				} else {
					layer.msg(data.msg);
				} 
			},
			error: function(error) {
				console.log("获取失败");
			}
		});
	}
	getCourseType();
	
	//选择课程分类
	window.chooseCourseType = function(id, name){
		$("#courseTypeId").val(id);
		$("#courseTypeName").val(name);
		$(".courseType").removeClass("layui-this");
		$("#courseType" + id).addClass("layui-this");
	};
	/*------------------取出所有的课程分类.end------------------*/
	
	/*------------------上传图片.start------------------*/
	var imgNum = 0,
		imgsArr = new Array();



	upload.render({
		elem: '#cover',
		url: url + 'attachment/upload',
		multiple: true,
		accept: 'images',
		exts: 'png|jpg|gif|jpeg|PNG|JPG|GIF|JPEG',
		before: function(obj) {
            index = layer.load(1, {
                shade: [0.3,'#000'] //0.1透明度的白色背景
            });
		},
		done: function(res) {
			//上传完毕
			if (res.code == 0) {
                layer.close(index);
				imgNum++;
				imgsArr = new Array();
				imgsArr.push(res.data.id);

				var imgHtml = $('<div style="display:inline-block;position:relative;margin-right:20px;" id="picupdata' + imgNum + '"  ><div onclick="delpic(\'picupdata' + imgNum + '\',\'' + imgNum + '\',\'' + res.data.id + '\')" style="position:absolute;top:-4px;right:-5px;color:white;background:red;border:1px solid red;border-radius:50%;width:16px;height:16px"><i class="layui-icon" style="position:relative;top:-2px;right:0px;">&#x1006;</i></div><img id="pic' + res.data.id + '" onclick="lookbigpic(\'' + res.data.id + '\')" src="' + url + 'attachment/seekFile/' + res.data.id + '" width="100px" height="100px" class="picsty"/></div>');
				$('#coverDiv').html(imgHtml);

			} else {
				layer.msg(res.msg);
			}
		}
	});
	/*------------------上传图片.end------------------ */
	
	/*------------------删除图片.start------------------*/
	window.delpic = function(self, num, inde) {
		layer.confirm('你确定要删除吗？', {
			btn: ['确定', '取消'] //按钮
		}, function() {
			$('#' + self).remove();
			imgsArr = new Array();
			layer.msg('删除成功');
		}, function() {
	
		});
	}
	/*------------------删除图片.end------------------ */
	
	/*------------------查看大图.start------------------*/
	window.lookbigpic = function(self) {
		var json = {
			"status": 1,
			"msg": "",
			"title": "",
			"id": 8,
			"start": 0,
			"data": [{
				"alt": "",
				"pid": 109,
				"src": url + "attachment/seekFile/" + self,
				"thumb": ""
			}]
		}
		layer.photos({
			photos: json,
			anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机
		});
	}
	/*------------------查看大图.end------------------ */
	
	/*------------------提交课程.start------------------ */
	$("#submitCourse").on('click', function() {
		addCourse();
	});
	
	function addCourse(){
		var name = $("#name").val();
		if (name == "") {
			layer.msg("请输入课程名称");
			return;
		}
		if (fileArr.length == 0) {
			layer.msg("请上传课程资料");
			return;
		}
		// if (isPpt == true && isPptImgIds == "") {
		// 	layer.msg("课程资料上传出错，请重新上传");
		// 	return;
		// }
		var postTypeId = $("#postTypeId").val();
		if (!postTypeId) {
			layer.msg("请选择业务分类");
			return;
		}
		var courseTypeId = $("#courseTypeId").val();
		if (!courseTypeId) {
			layer.msg("请选择课程分类");
			return;
		}
		// if (imgsArr.length == 0) {
		// 	layer.msg("请上传封面图片");
		// 	return;
		// }
		var postUrl = "";
		var str = "";
		if (courseId != false) {
			str = "编辑课程成功！";
			postUrl = url + 'course/edit';
			param = {Token: salt,id: courseId,name: name,attId: fileArr.toString(),imgIds: isPptImgIds,postTypeId: postTypeId,courseTypeId: courseTypeId,imgId: imgsArr.toString()};
		} else {
			str = "上传课程成功！";
			postUrl = url + 'course/add';
			param = {Token: salt,name: name,attId: fileArr.toString(),imgIds: isPptImgIds,postTypeId: postTypeId,courseTypeId: courseTypeId,imgId: imgsArr.toString()};
		}
		$.ajax({
			url: postUrl,
			dataType: 'json',
			async: false,
			type: "POST",
			data: param,
			success: function(data) {
				if(data.code == 0) {
					var num = layer.confirm(str, {
						btn: ['确定'] //按钮
					}, function() {
						layer.close(num);
						history.go(-1);
					}, function() {
					});
				} else {
					layer.msg(data.msg);
				}
			},
			error: function(error) {
				console.log("获取失败");
			}
		});
		
	}
	/*------------------提交课程.end------------------ */
	
	/*------------------撤销修改.start------------------ */
	$("#undoModify").on("click", function(){
		unModify();
	});
	function unModify(){
		$('#dataDiv').empty();
		$('#coverDiv').empty();
		var num = 1;
		var fileHtml = $('<div style="display:inline-block;position:relative;margin-right:45px;" id="picupfile' + num + '"  ><div onclick="delfile(\'picupfile' + num + '\',\'' + num + '\',\'' + course.attId + '\')" style="position:absolute;top:-2px;right:-21px;color:red;width:16px;height:16px"><i class="layui-icon" style="position:relative;top:-2px;right:0px;">&#x1006;</i></div><a style="color: blue;text-decoration: underline;" href="' + url + 'attachment/seekFile/' + course.attId + '" >' + course.fileName + '</a></div>');
		$('#dataDiv').html(fileHtml);
		var idx = 1;
		var imgHtml = $('<div style="display:inline-block;position:relative;margin-right:20px;" id="picupdata' + idx + '"  ><div onclick="delpic(\'picupdata' + idx + '\',\'' + idx + '\',\'' + course.imgId + '\')" style="position:absolute;top:-4px;right:-5px;color:white;background:red;border:1px solid red;border-radius:50%;width:16px;height:16px"><i class="layui-icon" style="position:relative;top:-2px;right:0px;">&#x1006;</i></div><img id="pic' + course.imgId + '" onclick="lookbigpic(\'' + course.imgId + '\')" src="' + url + 'attachment/seekFile/' + course.imgId + '" width="100px" height="100px" class="picsty"/></div>');
		$('#coverDiv').html(imgHtml);
	}
	/*------------------撤销修改.end------------------ */
	
	
	/*------------------编辑情况.start------------------ */
	var courseId = getQueryVariable("id");
	if (courseId != false) {
		$("#pageTitle").html("编辑课程");
		initEditShow();
	} else {
		$("#pageTitle").html("上传课程");
	}
	var course;
	function initEditShow(){
		$.ajax({
			url: url + "course/getInfo",
			dataType: "json",
			async: true,
			type: "post",
			data: {
				Token: salt,
				id : courseId
			},
			success: function(data){
				if (data.code == 0) {
					course = data.data;
					$("#name").val(course.name);
					var num = 1;
					fileArr.push(course.attId);
					var fileHtml = $('<div style="display:inline-block;position:relative;margin-right:45px;" id="picupfile' + num + '"  ><div onclick="delfile(\'picupfile' + num + '\',\'' + num + '\',\'' + course.attId + '\')" style="position:absolute;top:-2px;right:-21px;color:red;width:16px;height:16px"><i class="layui-icon" style="position:relative;top:-2px;right:0px;">&#x1006;</i></div><a style="color: blue;text-decoration: underline;" href="' + url + 'attachment/seekFile/' + course.attId + '" >' + course.attName + '</a></div>');
					$('#dataDiv').html(fileHtml);
					if (course.fileExt == "ppt" || course.fileExt == "pptx") {
						isPpt = true;
					}
					isPptImgIds = course.imgIds;
					$("#postTypeId").val(course.postTypeId);
					$("#postTypeName").val(course.postTypeName);
					$("#courseTypeId").val(course.courseTypeId);
					$("#courseTypeName").val(course.courseTypeName);
					var idx = 1;
					imgsArr.push(course.imgId);
					var imgHtml = $('<div style="display:inline-block;position:relative;margin-right:20px;" id="picupdata' + idx + '"  ><div onclick="delpic(\'picupdata' + idx + '\',\'' + idx + '\',\'' + course.imgId + '\')" style="position:absolute;top:-4px;right:-5px;color:white;background:red;border:1px solid red;border-radius:50%;width:16px;height:16px"><i class="layui-icon" style="position:relative;top:-2px;right:0px;">&#x1006;</i></div><img id="pic' + course.imgId + '" onclick="lookbigpic(\'' + course.imgId + '\')" src="' + url + 'attachment/seekFile/' + course.imgId + '" width="100px" height="100px" class="picsty"/></div>');
					$('#coverDiv').html(imgHtml);
					
					$("#undoModify").removeClass("layui-hide").addClass("layui-show");
				} else {
					setTimeout(function(){
						layer.msg(data.msg);
					}, 1000);
					courseId = false;
				}
			},
			error: function(error){
				console.log("获取失败");
			}
		});
	}
	/*------------------编辑情况.end------------------ */
});